package com.cmwa.solution.sys.permission.operRole.service.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cmwa.solution.base.entity.Employee;
import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.exception.RepositoryException;
import com.cmwa.solution.sys.menu.dao.MenuDao;
import com.cmwa.solution.sys.permission.dataAuthority.entity.RoleVo;
import com.cmwa.solution.sys.permission.dataRole.dao.RoleDao;
import com.cmwa.solution.sys.permission.operRole.dao.OperRoleDao;
import com.cmwa.solution.sys.permission.operRole.dao.OperRoleMenuDao;
import com.cmwa.solution.sys.permission.operRole.entity.OperRoleVo;
import com.cmwa.solution.sys.permission.operRole.service.OperRoleService;
import com.cmwa.solution.utils.SessionUtils;

@Service
public class OperRoleServiceImpl implements OperRoleService{
	
	@Resource
	private OperRoleDao operRoleDao;
	
	@Resource
	private MenuDao menuDao;
	
	@Resource
	private OperRoleMenuDao operRoleMenuDao;
	
	@Resource
	private RoleDao roleDao;

	@Override
	public Page<OperRoleVo> roleListPage(SearchParam param) {
		//去空格
		String roleName = (String)param.getSp().get("roleName");
		if (roleName != null && roleName != "") {
			roleName = roleName.trim();
		}
		param.getSp().put("roleName", roleName);
		Employee employee = SessionUtils.getEmployee();
		param.getSp().put("createId", employee.getID());
		List<OperRoleVo> items = operRoleDao.roleListPage(param);
		return Page.create(items, param.getStart(), param.getLimit(), param.getTotal());
	}
	

	@Override
	public OperRoleVo queryRoleById(String roleId) {
		return operRoleDao.queryRoleById(roleId);
	}
	

	@Override
	public boolean checkRepeatRole(String roleName,String roleId) {
		// 去空格
		if (roleName != null ) {
			roleName.trim(); 
		}
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("roleName", roleName);
		param.put("roleId", roleId);
		int count = operRoleDao.getCountByRoleName(param);
		return count > 0;
	}

	@Override
	public void saveRole(OperRoleVo roleVo) throws RepositoryException {
		// 去空格
		if (roleVo != null && roleVo.getRoleName() != null) {
			roleVo.getRoleName().trim(); 
		}
		//初始化参数
		preInsert(roleVo);
		operRoleDao.saveRole(roleVo);
		
	}
	
	@Override
	public void updateRole(OperRoleVo roleVo) {
		preUpdate(roleVo);
		operRoleDao.update(roleVo);
	}
	

	@Override
	public int deleteRoleWithStatus(String roleId) {
		OperRoleVo roleVo = new OperRoleVo();
		roleVo.setRoleId(roleId);
		roleVo.setStatus("D");
		return operRoleDao.update(roleVo);
	}
	
	@Override
	public List<OperRoleVo> queryAllRole(String roleName) {
		return operRoleDao.roleList(roleName);
	}
	
	/**
	 * insert初始化参数
	 * @param roleVo
	 */
	private void preInsert(OperRoleVo roleVo){
		String id = SessionUtils.getEmployee().getID();
		roleVo.setCreateId(id);
	}
	
	/**
	 * update初始化参数
	 * @param roleVo
	 */
	private void preUpdate(OperRoleVo roleVo){
		String id = SessionUtils.getEmployee().getID();
		roleVo.setModifyId(id);
	}


	@Override
	public List<RoleVo> getDataRole(String roleId) {
		return roleDao.getDataRole(roleId);
	}


	@Override
	public Page<RoleVo> roleDetailListPage(SearchParam param) {
		List<RoleVo> items = roleDao.getDataRoleListPage(param);
		return Page.create(items, param.getStart(), param.getLimit(), param.getTotal());
	}




	


	


	






}
